package com.eastfair.point.comsumer;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.eastfair.auth.api.AuthServiceFeign;
import com.eastfair.auth.api.UserAccountServiceFeign;
import com.eastfair.auth.dto.UserAccountDto;
import com.eastfair.businpoint.*;
import com.eastfair.core.base.R;
import com.eastfair.core.businpoint.BusinBasePoint;
import com.eastfair.core.context.ContextUtil;
import com.eastfair.core.point.BasePoint;
import com.eastfair.point.PointKeyDefinition;
import com.eastfair.point.UserLoginPoint;
import com.eastfair.point.entity.TestCollection;
import com.eastfair.point.service.TestCollectionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

import java.time.ZoneId;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Slf4j
@Component
public class BusinConsumerListener {

    @Autowired
    AuthServiceFeign authServiceFeign;

    @Autowired
    UserAccountServiceFeign userAccountServiceFeign;
    @KafkaListener(topics = "${ef.kafka.businEvent}",groupId = "${spring.application.name}")
    public void onMessage(String message) {
        // TODO: 2022/1/21 kafaka业务埋点记录处理
        BusinBasePoint businBasePoint = JSONObject.parseObject(message, BusinBasePoint.class);
        JSONObject jsonObject = (JSONObject) businBasePoint.getPointObj();
        String businPointName = jsonObject.getString("businPointName");

        log.info("BusinConsumerListener is execute message is "+message);
        if (businPointName.equals(BusinPointKeyDefinition.LOAD_USER_PERMISS)) {
            BusinLoadUserPermissPoint businLoadUserPermissPoint = JSON.toJavaObject((JSON) businBasePoint.getPointObj(), BusinLoadUserPermissPoint.class);
            //加载用户权限事件埋点
            try{
                ContextUtil.setProjectId(businLoadUserPermissPoint.getProjectId());
                authServiceFeign.loadUserPermission(businLoadUserPermissPoint.getUserIdList());
            }catch (Exception e){
                e.printStackTrace();
            }

        }else if (businPointName.equals(BusinPointKeyDefinition.LOAD_USER_PERMISS_OF_ORG)) {
            BusinLoadUserPermissOfOrgPoint businLoadUserPermissOfOrgPoint = JSON.toJavaObject((JSON) businBasePoint.getPointObj(), BusinLoadUserPermissOfOrgPoint.class);
            //加载用户权限事件埋点
            try{
                ContextUtil.setProjectId(businLoadUserPermissOfOrgPoint.getProjectId());
                authServiceFeign.loadUserPermissionOfOrg(businLoadUserPermissOfOrgPoint.getOrgIdList());
            }catch (Exception e){
                e.printStackTrace();
            }

        }else if (businPointName.equals(BusinPointKeyDefinition.LOAD_USER_PERMISS_OF_ROLE)) {
            BusinLoadUserPermissOfRolePoint businLoadUserPermissOfRolePoint = JSON.toJavaObject((JSON) businBasePoint.getPointObj(), BusinLoadUserPermissOfRolePoint.class);
            //加载用户权限事件埋点
            try{
                ContextUtil.setProjectId(businLoadUserPermissOfRolePoint.getProjectId());
                authServiceFeign.loadUserPermissionOfRole(businLoadUserPermissOfRolePoint.getRoleIdList());
            }catch (Exception e){
                e.printStackTrace();
            }

        }else if (businPointName.equals(BusinPointKeyDefinition.LOAD_USER_RELEVANCE)) {
            BusinLoadUserRelevancePoint businLoadUserRelevancePoint = JSON.toJavaObject((JSON) businBasePoint.getPointObj(), BusinLoadUserRelevancePoint.class);
            //加载用户关联事件埋点
            try{
                UserAccountDto userAccountDto = new UserAccountDto();
                BeanUtils.copyProperties(businLoadUserRelevancePoint,userAccountDto);
                userAccountDto.setId(businLoadUserRelevancePoint.getUserId());
                ContextUtil.setProjectId(businLoadUserRelevancePoint.getProjectId());
                userAccountServiceFeign.updateCorrelationById(userAccountDto);
            }catch (Exception e){
                e.printStackTrace();
            }

        }
    }
}
